package com.xxxx.server.controller;

import com.xxxx.server.pojo.Admin;
import com.xxxx.server.pojo.AdminLoginParam;
import com.xxxx.server.service.IAdminService;
import com.xxxx.server.util.RespBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * @Author:王亚栋
 * @Date: 2023/6/11 下午9:14
 * @Version:1.0
 */
@RestController
@Api(tags = "LoginController")
public class LoginController {

    @Autowired
    private IAdminService adminService;

    @ApiOperation(value = "登录之后返回token")
    @PostMapping("/login")
    public RespBean login(@RequestBody AdminLoginParam param,
                          HttpServletRequest request){

        return adminService.login(param.getCode(),
                param.getUsername(),
                param.getPassword(),
                request);
    }

    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public RespBean logout(){

        return RespBean.success("注销成功！");
    }

    @ApiOperation(value = "获取当前登录用户的信息")
    @GetMapping("/admin/info")
    public Admin getAdminInfo(Principal principal){

        if (null == principal){

            return null;
        }

        String username = principal.getName();

        Admin admin =  adminService.getAdminByUsername(username);

        admin.setPassword(null);

        admin.setRoles(adminService.getRoles(admin.getId()));

        return admin;

    }
}
